<?php
namespace Home\Controller;
use Home\Common\Common;
class DptController extends Common{
    public function index(){
        $dpt = M('departmentinfo');  //直接实例化表为模型
        if (empty($_GET['search'])){
            $where = array('departmentinfo.state'=>1);
        }else{
            $where = array('`dpt_name` like "%'.$_GET['value'].'%" or `dpt_bility` like "%'.$_GET['value'].'%"','departmentinfo.state'=>1);
            $this->assign('value',$_GET['value']);
        }
        //统计输出到页面的数据总条数
        $count = $dpt->where($where)->count();
        //实例化thinkphp的分页类
        $page = new \Think\Page($count,4);
        //输出分页
        $show = $page->show();
        //取出当前页的数据
        $list = $dpt->where($where)->limit($page->firstRow.','.$page->listRows)->order('dpt_id')->select();
        //将计数的数据输出到视图
        $this->assign('count',$count);
        //将分页信息输出到视图
        $this->assign('show',$show);
        //将数据输出到视图
        $this->assign('list',$list);
        $this->display();
    }
    //添加部门
    public function add(){
        if (empty($_POST['submit'])) {
            $dpti=  M('departmentinfo');
            $dtp=$dpti->where('`state`=1')->select();
            $this->assign('dtp',$dtp);
            $this->display();
        }else{
            $pt = M('departmentinfo');
            if (empty($_POST['dpt_name']))$this->error('部门名称不能为空',U('add'));
            $pt->add($_POST);
            $this->success('部门添加成功',U('index'));
        }
    }
    //隐藏部门
    public function del(){
        if(empty($_GET['dpt_id'])){
            $this->error('参数错误哟',U('index'));
        }
        else{
            $ddpt=M('departmentinfo');
            $ddpt->where('dpt_id='.$_GET['dpt_id'])->save(array('state'=>0));
            $this->success('成功隐藏部门',U('index'));
        }
    }
    //修改部门信息
    public function edit(){
        if (empty($_POST['submit'])){
            $tp=  M('departmentinfo');
            $tpt=$tp->where('`state`=1')->select();
            $this->assign('tpt',$tpt);
            if (empty($_GET['dpt_id'])) {
                $this->error('参数错误', U('index'));
            }
            $ment = M('departmentinfo');
            $info = $ment->where('`dpt_id`='.$_GET['dpt_id'])->find();
            $this->assign('info',$info);
            $this->display();
        }else{
            $role = D('departmentinfo');
            $role->where('`dpt_id`='.$_POST['dpt_id'])->save($_POST);
            $this->success('修改成功',U('index'));
        }
    }
    //批量隐藏
    public function hide_all(){
        $dptm = M('departmentinfo');
        if(empty($_GET['dpt_id'])){
            $this->error('请选择需要隐藏的内容',U('index'));
        }
        $dpt_id= implode(',',$_GET['dpt_id']);//获取id
        $str = explode(",",$dpt_id);//去逗号
        foreach ($str as $a){//循环id
            $dptm->where(array('dpt_id'=>$a))->save(array('state'=>0));
        }
        $this->success('隐藏成功',U('index'));
    }
    //回收站
    public function new_index(){
        $dpt = M('departmentinfo');  //直接实例化表为模型
        if (empty($_GET['search'])){
            $where = array('departmentinfo.state'=>0);
        }else{
            $where = array('`dpt_name` like "%'.$_GET['select'].'%" or `dpt_bility` like "%'.$_GET['select'].'%"','departmentinfo.state'=>0);
            $this->assign('value',$_GET['value']);
        }
        //统计输出到页面的数据总条数
        $count = $dpt->where($where)->count();
        //实例化thinkphp的分页类
        $page = new \Think\Page($count,3);
        //输出分页
        $show = $page->show();
        //取出当前页的数据
        $list = $dpt->where($where)->limit($page->firstRow.','.$page->listRows)->order('dpt_id')->select();
        //将计数的数据输出到视图
        $this->assign('count',$count);
        //将分页信息输出到视图
        $this->assign('show',$show);
        //将数据输出到视图
        $this->assign('list',$list);
        $this->display();
    }
    //清空
    public function del_all(){
        $dptm = M('departmentinfo');
        if(empty($_GET['dpt_id'])){
            $this->error('请选择需要隐藏的内容',U('index'));
        }
        $dpt_id= implode(',',$_GET['dpt_id']);//获取id
        $str = explode(",",$dpt_id);//去逗号
        foreach ($str as $a){//循环id
            $dptm->where(array('dpt_id'=>$a))->delete();
        }
        $this->success('清空成功',U('index'));
    }
    //显示已经隐藏的内容
    public function display_a(){
        if(empty($_GET['dpt_id'])){
            $this->error('参数错误哟',U('index'));
        }
        else{
            $ddpt=M('departmentinfo');
            $ddpt->where('dpt_id='.$_GET['dpt_id'])->save(array('state'=>1));
            $this->success('ok了',U('index'));
        }
    }
    //删除
    public function delete(){
        if(empty($_GET['dpt_id'])){
            $this->error('参数错误哟',U('index'));
        }
        else{
            $ddpt=M('departmentinfo');
            $ddpt->where('dpt_id='.$_GET['dpt_id'])->delete();
            $this->success('成功删除',U('index'));
        }
    }
}